//
//  NBADataManager+Player.swift
//  NBA
//
//  Created by tiger on 2018/2/3.
//  Copyright © 2018年 Meigabay. All rights reserved.
//

import UIKit
import SQLite

internal class NBAPlayerTableColumn: NBAWorkerTableColumn {
    //固定信息
    let clubId = Expression<String>("clubId")
    let number = Expression<Int>("number")
    let position = Expression<Int>("position")
    
    let twoPoint = Expression<Double>("twoPoint")
    let threePoint = Expression<Double>("threePoint")
    let controlAbility = Expression<Double>("controlAbility")
    let passAbility = Expression<Double>("passAbility")
    let handlingStressAbility = Expression<Double>("handlingStressAbility")
    
    let bounceSpeed = Expression<Double>("bounceSpeed")
    let bounceHeight = Expression<Double>("bounceHeight")
    let runSpeed = Expression<Double>("runSpeed")
    let thinkingSpeed = Expression<Double>("thinkingSpeed")
    let physicalStrength = Expression<Double>("physicalStrength")
}


extension NBADBManager {
    
    internal func createPlayerTable() throws {
        let playerColumns = NBAPlayerTableColumn()
        let createDownloadVideoTable = self.nbaPlayerTable.create(ifNotExists: true) { (table) in
            table.column(playerColumns.workerId,primaryKey: true)
            table.column(playerColumns.avatar)
            table.column(playerColumns.name)
            table.column(playerColumns.nickname)
            table.column(playerColumns.gender)
            table.column(playerColumns.height)
            table.column(playerColumns.weight)
            table.column(playerColumns.birthDay)
            table.column(playerColumns.drafted)
            table.column(playerColumns.introduction)
            table.column(playerColumns.createTime)
            table.column(playerColumns.type)
            table.column(playerColumns.clubId)
            table.column(playerColumns.number)
            table.column(playerColumns.position)
            table.column(playerColumns.twoPoint)
            table.column(playerColumns.threePoint)
            table.column(playerColumns.controlAbility)
            table.column(playerColumns.passAbility)
            table.column(playerColumns.handlingStressAbility)
            table.column(playerColumns.bounceSpeed)
            table.column(playerColumns.bounceHeight)
            table.column(playerColumns.runSpeed)
            table.column(playerColumns.thinkingSpeed)
            table.column(playerColumns.physicalStrength)
        }
        do {
            try self.db.run(createDownloadVideoTable)
            LogUtil.i("created player table")
        } catch {
            LogUtil.e(error)
            throw(error)
        }
    }
    
    internal func insertPlayer(player: NBAPlayer) throws {
        let columns = NBAPlayerTableColumn()
        let insertPlayer = self.nbaPlayerTable.insert(columns.workerId <- player.workerId,
                                                  columns.clubId <- player.clubId,
                                                  columns.avatar <- player.avatar,
                                                  columns.name <- player.name,
                                                  columns.nickname <- player.nickname,
                                                  columns.number <- player.number,
                                                  columns.position <- player.position.rawValue,
                                                  columns.gender <- player.gender.rawValue,
                                                  columns.height <- player.height,
                                                  columns.weight <- player.weight,
                                                  columns.birthDay <- player.birthDay,
                                                  columns.drafted <- player.drafted,
                                                  columns.introduction <- player.introduction,
                                                  columns.createTime <- Date().timeIntervalSince1970,
                                                  columns.type <- player.type.rawValue,
                                                  columns.twoPoint <- player.twoPoint,
                                                  columns.threePoint <- player.threePoint,
                                                  columns.controlAbility <- player.controlAbility,
                                                  columns.passAbility <- player.passAbility,
                                                  columns.handlingStressAbility <- player.handlingStressAbility,
                                                  columns.bounceSpeed <- player.bounceSpeed,
                                                  columns.bounceHeight <- player.bounceHeight,
                                                  columns.runSpeed <- player.runSpeed,
                                                  columns.thinkingSpeed <- player.thinkingSpeed,
                                                  columns.physicalStrength <- player.physicalStrength)
        do {
            try self.db.run(insertPlayer)
            LogUtil.i("Insert Player Success")
        } catch {
            LogUtil.e(error)
            throw(error)
        }
    }
    
    internal func filterClubPlayers(clubId: String) -> [NBAPlayer] {
        let colums = NBAPlayerTableColumn()
        let filterPlayer = self.nbaPlayerTable.filter(colums.clubId == clubId)
        do {
            var data = [NBAPlayer]()
            let results = try self.db.prepare(filterPlayer)
            for result in results {
                let player = NBAPlayer(result: result)
                data.append(player)
            }
            return data;
        } catch {
            LogUtil.i(error)
            return [];
        }
    }
    
}
